//
//  XQprivacyView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/22.
//

#import "XQprivacyView.h"
#import "XQPrivacyPopView.h"
#import "XQPopupAnimView.h"

@interface XQprivacyView()
@property (nonatomic,strong) UILabel *priacyLb;
@property (nonatomic,strong) UIButton *privacyBtn;
@property (nonatomic,weak) UIViewController *controller;
@end

@implementation XQprivacyView

- (instancetype)initWithViewController:(UIViewController *)controller {
    self = [super init];
    if (self) {
        _controller = controller;
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.radioButton];
    [self addSubview:self.priacyLb];
    [self addSubview:self.privacyBtn];
}

- (void)makeConstraints {
    [self.radioButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@1);
        make.left.equalTo(@0);
        make.bottom.equalTo(@-1);
        make.width.height.mas_equalTo(16);
    }];
    [self.priacyLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.radioButton.mas_right).offset(6.5);
        make.centerY.equalTo(self.radioButton);
    }];
    [self.privacyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.radioButton);
        make.left.equalTo(self.priacyLb.mas_right);
        make.right.equalTo(@0);
    }];
}

#pragma mark - setter

- (void)setPrivacyPrefix:(NSString *)privacyPrefix {
    self.priacyLb.text = privacyPrefix;
}

- (void)setPrivacyValue:(NSString *)privacyValue {
    [self.privacyBtn setTitle:privacyValue forState:UIControlStateNormal];
    self.privacyBtn.hidden = privacyValue.length == 0;
}

#pragma mark - private

- (void)radioClick {
    self.radioButton.selected = !self.radioButton.selected;
    if (self.radioHandle) {
        self.radioHandle(self.radioButton.selected);
    }
}

- (void)privacyClick {
    
    if (self.privacyHandle) {
        self.privacyHandle();
        return;;
    }
    
    XQPrivacyPopView *popView = [[XQPrivacyPopView alloc] init];
    
    XQPopupAnimView *anim = [[XQPopupAnimView alloc] initWithAnimationType:XQPopupAnimationCenter customView:popView];
    anim.needCover = YES;
    anim.makeCustomConstraints = ^(XQPopupAnimView *animView, UIView *customView) {
        [customView mas_remakeConstraints:^(MASConstraintMaker *make) {
            (void)make.center;
        }];
    };
    
    [anim showInView:self.controller.view completion:^{}];
    
    @weakify(anim)
    popView.confimHandle = ^{
        @strongify(anim);
        [anim hideAndCompletion:^{}];
    };
}

#pragma mark - lazy

- (UIButton *)radioButton {
    if (!_radioButton) {
        _radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_radioButton setImage:[UIImage imageNamed:@"login_radio_unselect"] forState:UIControlStateNormal];
        [_radioButton setImage:[UIImage imageNamed:@"login_radio_select"] forState:UIControlStateSelected];
        [_radioButton addTarget:self action:@selector(radioClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _radioButton;
}

- (UILabel *)priacyLb {
    if (!_priacyLb) {
        _priacyLb = [[UILabel alloc] init];
        _priacyLb.textColor = kBlackTextColor;
        _priacyLb.font = [UIFont systemFontOfSize:13];
        _priacyLb.text = @"I have read and agree with ";
    }
    return _priacyLb;
}

- (UIButton *)privacyBtn {
    if (!_privacyBtn) {
        _privacyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_privacyBtn setTitle:@"<Privacy agreement>" forState:UIControlStateNormal];
        [_privacyBtn setTitleColor:kBlueTextColor forState:UIControlStateNormal];
        _privacyBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_privacyBtn addTarget:self action:@selector(privacyClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _privacyBtn;
}

@end
